package com.lkx.service.impl;

import com.lkx.cache.SeckillRedisCache;
import com.lkx.controller.SeckillController;
import com.lkx.dao.OrderMapper;
import com.lkx.domain.OrderDO;
import com.lkx.limit.Limit;
import com.lkx.utils.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import com.lkx.dao.SeckillMapper;
import com.lkx.domain.SeckillDO;
import com.lkx.service.SeckillService;
import org.springframework.transaction.annotation.Transactional;


@Service
public class SeckillServiceImpl implements SeckillService {

	private final static Logger logger = LoggerFactory.getLogger(SeckillController.class);

	@Autowired
	private SeckillMapper seckillMapper;
	@Autowired
	private OrderMapper orderMapper;
	@Autowired
	private SeckillRedisCache seckillRedisCache;

	@Override
	public int getSeckillCount(Long seckillId){
		return seckillMapper.getSeckillCount(seckillId);
	}

	public int getSeckillOrderCount(Long seckillId){
		return orderMapper.getSeckillOrderCount(seckillId);
	}

    @Limit(key = "test", period = 10, count = 100, name="resource", prefix = "limit")
	@Transactional
	@Override
	public R startSeckill(Long seckillId, Long userId){
		int num = seckillMapper.getSeckillCount(seckillId);
		if(num > 0){
			SeckillDO seckill = new SeckillDO();
			seckill.setId(seckillId);
			seckill.setNum(num-1);
			seckillMapper.update(seckill);
			//生成用户订单
			OrderDO orderDO = new OrderDO();
			orderDO.setSeckillId(seckillId);
			orderDO.setUserId(userId);
			orderDO.setStatus(1);
			orderMapper.save(orderDO);
			return R.ok();
		}else{
			return R.error();
		}
	}


	@Transactional
	public synchronized R startSeckillRedis(Long seckillId, Long userId){
		int num = seckillRedisCache.getSeckillCount(seckillId);
		if(num > 0){
			seckillRedisCache.setSeckillCount(seckillId, (num-1));
			logger.info("===========>num:"+(num-1));
			//生成用户订单
			OrderDO orderDO = new OrderDO();
			orderDO.setSeckillId(seckillId);
			orderDO.setUserId(userId);
			orderDO.setStatus(1);
			orderMapper.save(orderDO);
			return R.ok();
		}else{
			return R.error();
		}
	}
	
	@Override
	public SeckillDO get(Long id){
		return seckillMapper.get(id);
	}
	
	@Override
	public List<SeckillDO> list(Map<String, Object> map){
		return seckillMapper.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return seckillMapper.count(map);
	}
	
	@Override
	public int save(SeckillDO seckill){
		return seckillMapper.save(seckill);
	}

	@Override
	public int resetNum(int num, Long seckillId){
		return seckillMapper.resetNum(num,seckillId);
	}
	
	@Override
	public int update(SeckillDO seckill){
		return seckillMapper.update(seckill);
	}
	
	@Override
	public int remove(Long id){
		return seckillMapper.remove(id);
	}
	
	@Override
	public int batchRemove(Long[] ids){
		return seckillMapper.batchRemove(ids);
	}
	
}
